package yunjiao.designpatterns.interpreter.expr;

import yunjiao.designpatterns.interpreter.Employee;

/**
 * TODO
 *
 * @author yangyunjiao
 */
public class SalaryExpression extends TerminalExpression {
    protected String salary;

    public SalaryExpression(String salary) {
        super();
        this.salary = salary;
    }

    @Override

    public boolean interpret(Employee context) {
        if (!Character.isDigit(salary.charAt(0))) {
            char operator = salary.charAt(0);
            return interpretIntSalary(context, salary.substring(1), operator);
        } else {
            return interpretIntSalary(context, salary, '=');
        }

    }


    private boolean interpretIntSalary(Employee context, String salary, char operator) {
        try {
            int intSalary = Integer.parseInt(salary);
            return switch (operator) {
                case '>' -> context.getSalary() > intSalary;
                case '<' -> context.getSalary() < intSalary;
                default -> context.getSalary() == intSalary;
            };
        } catch (NumberFormatException exp) {
            return false;
        }
    }


    @Override
    public String toString() {
        return "SalaryExpression [salary=" + salary + "]";
    }
}
